/**
 * JAVACC DEMO 1.0
 */
package com.apache.dict.service.plugins;

import com.apache.api.api.PlatformService;
import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.cache.util.Validator;
import com.apache.dict.entity.DataItem;
import com.apache.dict.vo.ItemListVo;
import com.apache.tools.StrUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * description:  外部系统select控件加载数据字典数据
 */
public class WsDictSelectPlugin implements PluginConnector {

    private Logger logger = LoggerFactory.getLogger(getClass());

    private PlatformService<DataItem> dictInnerService;

    /**
     * @see com.apache.api.manager.PluginConnector#execute(com.apache.api.vo.ParamsVo)
     */
    @Override
    public Object execute(ParamsVo arg0) throws Exception {
        ResultEntity result = new ResultEntity();
        result.setEntity(null);
        result.setMessage("查询失败！");
        result.setResult("false");

        String doCode = StrUtil.doNull( String.valueOf(arg0.getParams("doCode")),"getList");
        String cateEname = String.valueOf( arg0.getParams("cateEname"));
        String fatherValue = String.valueOf( arg0.getParams("fatherValue"));
        this.logger.info("cateEname=" + cateEname + ", fatherValue=" + fatherValue);
        if (Validator.isNull(cateEname) || Validator.isNull(fatherValue)) {
            result.setMessage("缺少必要的参数：cateEname或者fatherValue为空！");
            return result;
        }
        if("getList".equals(doCode)){
            getList(result,arg0);
        }else if("getTreeList".equals(doCode)){
            getTreeList(result,arg0);
        }
        return result;
    }
    private void getList(ResultEntity result,ParamsVo vo){
        String cateEname = String.valueOf( vo.getParams("cateEname"));
        String fatherValue = String.valueOf( vo.getParams("fatherValue"));
        ItemListVo item = new ItemListVo();
        item.setCateEname(cateEname);
        item.setFatherValue(fatherValue);
        String methodKey = "getList";
        vo.setMethodKey(methodKey);
        vo.setObj(item);

        String jsonStr = dictInnerService.doService(vo);
        if (Validator.isNotNull(jsonStr)) {
            result.setEntity(jsonStr);
            result.setMessage("查询成功！");
            result.setResult("true");
        }
    }
    private void getTreeList(ResultEntity result,ParamsVo vo){
        String cateEname = String.valueOf( vo.getParams("cateEname"));
        ItemListVo item = new ItemListVo();
        item.setCateEname(cateEname);
        String methodKey = "getTreeList";
        vo.setMethodKey(methodKey);
        vo.setObj(item);

        String jsonStr = dictInnerService.doService(vo);
        if (Validator.isNotNull(jsonStr)) {
            result.setEntity(jsonStr);
            result.setMessage("查询成功！");
            result.setResult("true");
        }
    }
    public void setDictInnerService(PlatformService<DataItem> dictInnerService) {
        this.dictInnerService = dictInnerService;
    }

}
